package com.hnevc.trafficdemotest1.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.hnevc.trafficdemotest1.R;

import org.json.JSONException;
import org.json.JSONObject;

public class LoginActivity extends AppCompatActivity {
    String urls = "http://192.168.1.142:8080/RestaurantServer/user/doLogin";
    private static final String TAG = "LoginActivity";
    RequestQueue requestQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
    }

    public void doLogin(View view) {

        TextView mTvUserName = (TextView) findViewById(R.id.id_et_username);
        TextView mTvPassword = (TextView) findViewById(R.id.id_et_password);
        JSONObject object = new JSONObject();
        try {
            object.put("userName",mTvUserName.getText().toString());
            object.put("userPassword",mTvPassword.getText().toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }


        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, urls,object,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {

                        String result = jsonObject.optString("RESULT");

                        if(result.equals("S")){
                            String userRole = jsonObject.optString("UserRole");
                            Toast.makeText(LoginActivity.this, jsonObject.optString("ERRMSG")+",角色为:"+userRole, Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                            startActivity(intent);
                            finish();
                        }else if(result.equals("F")){
                            Toast.makeText(LoginActivity.this, jsonObject.optString("ERRMSG"), Toast.LENGTH_SHORT).show();
                        }
                        else{
                            Toast.makeText(LoginActivity.this, "登录错误", Toast.LENGTH_SHORT).show();
                        }

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.e(TAG, "onErrorResponse: "+volleyError.toString() );
                        Toast.makeText(LoginActivity.this, "登录错误", Toast.LENGTH_SHORT).show();
                    }
                });
        requestQueue = Volley.newRequestQueue(this);

        requestQueue.add(jsonObjectRequest);
    }
}
